import faces from "@/assets/js/face";

const escapeHtml = (val = "") => {
  // debugger
  if (!val) return "";
  //替换表情
  let result = val;
  const face = /\sface\[\W{1,}]/g;
  if (face.test(result)) {
    // debugger
    let group = result.match(face);
    group.map((item) => {
      const key = item.match(/\[\S+\]/g)[0];
      result = result.replace(item, `<img src="${faces[key]}">`);
    });
  }
  //图片替换
  const img = /img\S+\]/g;
  if (img.test(result)) {
    const group = result.match(img);
    group.map((item) => {
      result = result.replace(item, `<img src="${item.slice(4, -1)}">`);
    });
  }
  //连接替换
  const Link = /a\(\S+\]/g;
  if (Link.test(result)) {
    const group = result.match(Link);
    const title = /\((.+)\)/;
    const LinkName = /\[(.+)\]/;
    group.map((item) => {
      const nameGroup = item.match(LinkName);
      let name = "";
      if (nameGroup.length > 0) {
        name = nameGroup[1];
      }
      const linkGroup = item.match(title);
      let Link = "";
      if (linkGroup.length > 0) {
        Link = linkGroup[1];
      }
      result = result.replace(item, `<a href="${Link}">${name}</a>`);
    });
  }

  //引用替换
  result = result.replace(/\[quote\]/g, '<div class="layui-elm-quote">');
  result = result.replace(/\[\/quote\]/g, "</div>");
  //代码块替换
  //hr替换
  return result;
};

export { escapeHtml };
